//
//  ImageCollectionViewController.m
//  MyNewWeiBo
//
//  Created by wpapple on 16/6/4.
//  Copyright © 2016年 heron. All rights reserved.
//

#import "ImageCollectionViewController.h"
#import "ImageCollectionViewCell.h"

@interface ImageCollectionViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>



@end

@implementation ImageCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addImage:) name:KImageAddBtnClick object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleImage:) name:KImageDelBtnClick object:nil];
    
}

- (void)addImage:(NSNotification *)notification{
    UIImagePickerController * imagePickC = [[UIImagePickerController alloc]init];
    [self presentViewController:imagePickC animated:YES completion:nil];
    imagePickC.delegate = self;
}

- (void)deleImage:(NSNotification *)notification{

    
    ImageCollectionViewCell * cell = notification.object;
    if (cell.indexPath == nil) {
        return;
    }
    NSIndexPath * indexPath = cell.indexPath;
    [self.imageArray removeObjectAtIndex:indexPath.item];
    [self.collectionView reloadData];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage * image = info[UIImagePickerControllerOriginalImage];
    [self.imageArray addObject:image];
    [self.collectionView reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return self.imageArray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    NSLog(@"%ld",indexPath.row);
    NSLog(@"%ld",indexPath.item);
    if (indexPath.item == self.imageArray.count) {
        [cell.imageBtn setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
    }else{
        UIImage * image = self.imageArray[indexPath.item];
        cell.indexPath = indexPath;
        [cell.imageBtn setImage:image forState:UIControlStateNormal];
    }
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
